// pages/kid/kid.js

var app = getApp()
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    parentname: '', 
    childname: '',
    childcode: '',
    userid: ''
  }, 

  onShow: function() {
    var me = this;
    wx.getStorage({
      key: 'userid',
      success: function(res) {
        me.setData({
          userid: res.data,
        })
        console.log(me.data.userid) 
      },
    })
  },
  check() {
    if (this.data.parentname == '') {
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.childname == '') {
      wx.showToast({
        title: '请输入宝贝姓名',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.childcode == '') {
      wx.showToast({
        title: '请输入学号',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    return true;
  },

  formSubmit: function(e) {
    var me = this;
    var formObject = e.detail.value;
    var parentname = formObject.parentname;
    var childname = formObject.childname;
    var childcode = formObject.childcode;

   
    me.setData({
      parentname: parentname,
      childname: childname,
      childcode: childcode
    })
    console.log(parentname)
    console.log(childcode)
    console.log(childname)
    if (me.check()) {
      console.log('+++==')
      console.log(me.data.userid)
      wx.request({
        url: app.serverUrl + '/bindChild',
        data: {
          userid: me.data.userid,
          childcode: childcode,
          parentname: parentname,
          childname: childname,

        },
        method: 'POST',
        header: {
          "Content-Type": "application/json",
        },
        success: function(res) {
          console.log(res.data)
          if (res.data.status == 200) {
            wx.showToast({
              title: '绑定成功',
              icon: 'success'
            })
            setTimeout(function() {
              wx.hideToast()
            }, 2000)
            app.isNavigate = true;
            childname = JSON.stringify(childname);
            childcode = JSON.stringify(childcode);
            wx.redirectTo({
              url: '/pages/childList/childList?childname=' + childname +
                '&childcode=' + childcode,

            })
          } else if (res.data.status == 500) {
            wx.showToast({
              title: res.data.msg,
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})